package org.ru.mse10.cvis.entity.codelists;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.MapKey;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.ru.mse10.cvis.constants.CodeValueQueries;
import org.ru.mse10.cvis.entity.misc.Identity;
import org.ru.mse10.cvis.entity.misc.TranslatedValue;

/**
 * The Class CodelistValue.
 */
@Entity
@Table(name = "codelist_value")
@NamedQueries(value = { @NamedQuery(name = CodeValueQueries.QUERY_CV_OF_CL_KEY, query = CodeValueQueries.QUERY_CV_OF_CL) })
public class CodelistValue extends Identity {

	/** The codelist. */
	private Codelist codelist;

	/** The code. */
	private String code;

	/** The translated values. */
	private Map<String, TranslatedValue> translatedValues = new HashMap<String, TranslatedValue>();

	/**
	 * Gets the codelist.
	 * 
	 * @return the codelist
	 */
	@ManyToOne
	public Codelist getCodelist() {
		return codelist;
	}

	/**
	 * Sets the codelist.
	 * 
	 * @param codelist
	 *            the new codelist
	 */
	public void setCodelist(Codelist codelist) {
		this.codelist = codelist;
	}

	/**
	 * Gets the translated values.
	 * 
	 * @return the translated values
	 */
	@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL)
	@MapKey(name = "languageKey")
	public Map<String, TranslatedValue> getTranslatedValues() {
		return translatedValues;
	}

	/**
	 * Sets the translated values.
	 * 
	 * @param translatedValues
	 *            the translated values
	 */
	public void setTranslatedValues(
			Map<String, TranslatedValue> translatedValues) {
		this.translatedValues = translatedValues;
	}

	/**
	 * Gets the code.
	 * 
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * Sets the code.
	 * 
	 * @param code
	 *            the new code
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * Gets the values.
	 * 
	 * @return the values
	 */
	@Transient
	public List<TranslatedValue> getValues() {
		return new ArrayList<TranslatedValue>(getTranslatedValues().values());
	}

}
